<template>
  <div>
    <div>
      <el-input v-model="str" placeholder="根据类型名称查询" style="width: 300px; position: absolute; left: 270px;"></el-input>
      <el-button @click="so" style="position: absolute;left: 600px;">查询</el-button>
      <el-button @click="addCat" style="position: absolute;left: 670px;">新增</el-button>
      <el-button @click="deleteCat" style="position: absolute;left: 750px;">删除</el-button>
      <el-button @click="reload" style="position: absolute;left: 830px;">刷新</el-button>
    </div>
    <div class="table1">
      <el-table :data="GroupCatInfo" border :row-class-name="tableRowClassName" ref="multipleTable"
        @selection-change="handleSelectionChange">
        <!-- 表格中多选框组件 -->
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="groupName" label="类型名称" width="140">
        </el-table-column>
        <el-table-column prop="catName" label="适用大类" width="140">
        </el-table-column>
        <el-table-column prop="num" label="属性数量" width="120">
        </el-table-column>
        <el-table-column prop="groupStat" label="状态" width="120">
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button @click="updateGroup(scope.row.groupCode)" type="text" size="small">修改</el-button>
            <el-button @click="toparameter(scope.row.groupCode)" type="text" size="small">参数列表</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="GroupCatInfoPage.current" :page-sizes="[1,2,3]" :page-size="GroupCatInfoPage.size"
        layout="total, sizes, prev, pager, next, jumper" :total="GroupCatInfoPage.total">
      </el-pagination>
    </div>
    <!-- 修改 -->
    <el-dialog title="商品属性修改" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">

      <el-form :model="SaveGroupForm" :rules="rules" ref="SaveGroupForm" label-width="100px">
        <el-form-item label="类型名称" prop="groupName" style="width: 350px;">
          <el-input v-model="SaveGroupForm.groupName"></el-input>
        </el-form-item>
        <el-form-item label="是否适用" prop="groupStat">
          <el-radio-group v-model="SaveGroupForm.groupStat" style="position: absolute; left: 20px; top: 10px;">
            <el-radio label="启用"></el-radio>
            <el-radio label="停用"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-transfer v-model="SaveGroupForm.pCatName" :data="SaveGroupForm.pCatNames" style="text-align: left;">
        </el-transfer>
        <el-form-item>
          <el-button type="primary" @click="submitForm2('SaveGroupForm')">提交</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="qx2">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 新增 -->
    <el-dialog title="商品属性组新增" :visible.sync="dialogVisible1" width="50%" :before-close="handleClose">

      <el-form :model="UpdateGroupForm" :rules="rules" ref="UpdateGroupForm" label-width="100px">
        <el-form-item label="类型名称" prop="groupName" style="width: 350px;">
          <el-input v-model="UpdateGroupForm.groupName"></el-input>
        </el-form-item>
        <el-form-item label="是否适用" prop="groupStat">
          <el-radio-group v-model="UpdateGroupForm.groupStat" style="position: absolute; left: 20px; top: 10px;">
            <el-radio label="启用"></el-radio>
            <el-radio label="停用"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-transfer v-model="UpdateGroupForm.pCatName" :data="UpdateGroupForm.pCatNames" style="text-align: left;">
        </el-transfer>
        <el-form-item>
          <el-button type="primary" @click="submitForm('UpdateGroupForm')">提交</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="qx">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "group",
    data() {
      return {
        str: "",
        isChecked: [],
        GroupCatInfo: [],
        GroupCatInfoPage: {},
        dialogVisible: false,
        dialogVisible1: false,
        UpdateGroupForm: {
          groupName: "",
          groupStat: "",
          pCatName: [],
          pCatNames: []
        },
        SaveGroupForm: {
          groupCode: "",
          groupName: "",
          groupStat: "",
          //右
          pCatName: [],
          //左
          pCatNames: [],
        },
        rules: {
          groupName: [{
              required: true,
              message: '请输入类型名称',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 16,
              message: '长度在 2 到 16 个字符',
              trigger: 'blur'
            }
          ],
          groupStat: [{
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }],
        }
      }
    },
    methods: {
      //跳转参数页面
      toparameter(groupCode) {
        localStorage.setItem("groupCode", JSON.stringify(groupCode));
        this.$router.push("/parameter");
      },
      //新增
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //不为空发送新增请求
            this.$http.post("pawnAttrGroup/addNewGroup", {
              groupName: this.UpdateGroupForm.groupName,
              groupStat: this.UpdateGroupForm.groupStat,
              pcatName: this.UpdateGroupForm.pCatName
            }).then(resp => {
              alert(resp.data.message);
              location.reload();
            })
          } else {
            return false;
          }
        });
      },
      qx() {
        this.dialogVisible1 = false;
        location.reload();
      },
      getAllDProduct() {
        this.$http.get("pawnAttrGroup/getAllDProduct").then(resp => {
          let l = resp.data.data.pcatNames;
          for (let i in l) {
            this.UpdateGroupForm.pCatNames.push({
              key: l[i],
              label: l[i]
            });
          }
        })
      },
      addCat() {
        this.dialogVisible1 = true;
        //查询所有大类
        this.getAllDProduct();
      },
      //修改
      submitForm2(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //不为空发送修改请求
            this.$http.post("pawnAttrGroup/saveGroupByCode", {
              groupCode: this.SaveGroupForm.groupCode,
              groupName: this.SaveGroupForm.groupName,
              groupStat: this.SaveGroupForm.groupStat,
              pcatName: this.SaveGroupForm.pCatName
            }).then(resp => {
              alert(resp.data.message);
              location.reload();
            })
          } else {
            return false;
          }
        });
      },
      updateGroup(groupCode) {
        this.SaveGroupForm.groupCode = groupCode;
        //发送请求
        this.$http.get("pawnAttrGroup/getGroupByCode", {
          params: {
            groupCode: groupCode
          }
        }).then(resp => {
          this.SaveGroupForm.groupName = resp.data.data.groupName;
          this.SaveGroupForm.groupStat = resp.data.data.groupStat;
          let l = resp.data.data.pcatNames;
          for (let i in l) {
            this.SaveGroupForm.pCatNames.push({
              key: l[i],
              label: l[i]
            });
            if (l[i] == resp.data.data.pcatName) {
              this.SaveGroupForm.pCatName.push(l[i]);
            }
          }
        })
        this.dialogVisible = true;
      },
      handleClose(done) {
        location.reload();
      },
      qx2() {
        this.dialogVisible = false;
        location.reload();
      },

      handleSelectionChange(val) {
        this.isChecked = val;
      },
      deleteCat() {
        this.$http.post("pawnAttrGroup/deleteGroupByGid", {
          isChecked: this.isChecked
        }).then(resp => {
          alert(resp.data.message);
          location.reload();
        })
      },
      handleCurrentChange(current) {
        if (this.str == "") {
          this.getAllGroupCat(current, this.GroupCatInfoPage.size);
        } else {
          this.getByStr(current, this.GroupCatInfoPage.size);
        }
      },
      handleSizeChange(size) {
        if (this.str == "") {
          this.getAllGroupCat(this.GroupCatInfoPage.current, size);
        } else {
          this.getByStr(this.GroupCatInfoPage.current, size);
        }
      },
      tableRowClassName({
        row,
        rowIndex
      }) {
        if (row.groupStat === '停用') {
          return 'warning-row';
        } else {
          return 'success-row';
        }
        return '';
      },
      reload() {
        location.reload();
      },
      so() {
        this.getByStr(1, 1);
      },
      getByStr(currentPage, pagesize) {
        this.$http.get("pawnAttrGroup/getGroupCatByLike", {
          params: {
            str: this.str,
            current: currentPage,
            size: pagesize
          }
        }).then(resp => {
          if (resp.data.code == 200) {
            this.GroupCatInfo = resp.data.data.records;
            this.GroupCatInfoPage = resp.data.data;
          } else {
            alert(resp.data.message);
            location.reload();
          }
        })
      },
      getAllGroupCat(currentPage, pagesize) {
        this.$http.get("pawnAttrGroup/getAllGroupCat", {
          params: {
            current: currentPage,
            size: pagesize
          }
        }).then(resp => {
          if (resp.data.code == 200) {
            this.GroupCatInfo = resp.data.data.records;
            this.GroupCatInfoPage = resp.data.data;
          } else {
            alert(resp.data.message);
            location.reload();
          }
        })
      }
    },
    created() {
      this.getAllGroupCat(1, 3);
    }
  }
</script>

<style>
  .table1 {
    position: absolute;
    top: 100px;
    left: 150px;
  }

  .block {
    position: absolute;
    top: 400px;
    left: 300px;
  }

  .el-table .warning-row {
    background: yellow;
  }
</style>
